package jgl.vajra.management.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.guyj.BidirectionalSeekBar;

import jgl.vajra.management.R;

public class BidirectionalSeekBarView extends View {
    private int offset;
    private int withInColor;
    private int withOutColor;
    private int pb_height;
    private int leftSolidColor;
    private int rightSolidColor;
    private int leftStrokeColor;
    private int rightStrokeColor;
    private int ball_radius_size;
    private int ball_stroke_size;
    private int text_color;
    private int text_size;
    private int text_left_num;
    private int text_right_num;
    private int text_min_unit;
    private int totalLength;
    private String text_units;
    private TextPosition textPosition;
    private UnitsPosition unitPosition;
    private int ordinalTextPosition;
    private int ordinalUnitsPosition;
    private int leftBitmapResID;
    private int rightBitmapResID;
    private Paint withOutPaint;
    private Paint withInPaint;
    private Paint leftBallPaint;
    private Paint rightBallPaint;
    private Paint leftBallStrokePaint;
    private Paint rightBallStrokePaint;
    private Paint leftTextPaint;
    private Paint rightTextPaint;
    private RectF withOutRectF;
    private RectF withInRectF;
    private Rect leftTextRect;
    private Rect rightTextRect;
    private Rect leftSrcRect;
    private Rect rightSrcRect;
    private Rect leftDstRect;
    private Rect rightDstRect;
    private Paint bitmapPaint;
    private int ballY;
    private int leftBallX;
    private int rightBallX;
    private int textY;
    private int currentMovingType;
    private BidirectionalSeekBar.OnSeekBarChangeListener seekBarChangeListener;
    private int downX;
    private boolean isBitmap;
    private Bitmap leftBitmap;
    private Bitmap rightBitmap;
    private int leftBitmapL;
    private int leftBitmapT;
    private int leftBitmapR;
    private int leftBitmapB;
    private int rightBitmapL;
    private int rightBitmapT;
    private int rightBitmapR;
    private int rightBitmapB;
    private int lastX;
    public int leftProgress;
    public int rightProgress;

    public BidirectionalSeekBarView(Context context) {
        super(context);
    }

    public BidirectionalSeekBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initAttrs(context, attrs);
    }

    public BidirectionalSeekBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initAttrs(context, attrs);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BidirectionalSeekBar);
        this.withInColor = typedArray.getColor(R.styleable.BidirectionalSeekBar_pb_within_color, Color.parseColor("#111111"));
        this.withOutColor = typedArray.getColor(R.styleable.BidirectionalSeekBar_pb_without_color, Color.parseColor("#AAAAAA"));
        this.pb_height = typedArray.getDimensionPixelOffset(R.styleable.BidirectionalSeekBar_pb_height, 5);
        this.leftBitmapResID = typedArray.getResourceId(R.styleable.BidirectionalSeekBar_ball_left_drawable, 0);
        this.rightBitmapResID = typedArray.getResourceId(R.styleable.BidirectionalSeekBar_ball_right_drawable, 0);
        if (this.leftBitmapResID != 0 && this.rightBitmapResID != 0) {
            this.isBitmap = true;
        } else {
            this.leftSolidColor = typedArray.getColor(R.styleable.BidirectionalSeekBar_ball_left_solid_color, Color.parseColor("#118811"));
            this.rightSolidColor = typedArray.getColor(R.styleable.BidirectionalSeekBar_ball_right_solid_color, Color.parseColor("#118811"));
            this.leftStrokeColor = typedArray.getColor(R.styleable.BidirectionalSeekBar_ball_left_stroke_color, Color.parseColor("#777777"));
            this.rightStrokeColor = typedArray.getColor(R.styleable.BidirectionalSeekBar_ball_right_stroke_color, Color.parseColor("#777777"));
            this.ball_radius_size = typedArray.getDimensionPixelOffset(R.styleable.BidirectionalSeekBar_ball_radius_size, 30);
            this.ball_stroke_size = typedArray.getDimensionPixelOffset(R.styleable.BidirectionalSeekBar_ball_stroke_size, 0);
        }

        this.text_left_num = typedArray.getInt(R.styleable.BidirectionalSeekBar_text_left_num, 0);
        this.text_right_num = typedArray.getInt(R.styleable.BidirectionalSeekBar_text_right_num, 100);
        this.text_min_unit = typedArray.getInt(R.styleable.BidirectionalSeekBar_text_min_unit, 1);
        typedArray.recycle();
        this.currentMovingType = 16;
        this.offset = 1;
        this.withOutPaint = this.createPaint(this.withOutColor, 0, Paint.Style.FILL, 0);
        this.withInPaint = this.createPaint(this.withInColor, 0, Paint.Style.FILL, 0);
        if (!this.isBitmap) {
            this.leftBallPaint = this.createPaint(this.leftSolidColor, 0, Paint.Style.FILL, 0);
            this.rightBallPaint = this.createPaint(this.rightSolidColor, 0, Paint.Style.FILL, 0);
            this.leftBallStrokePaint = this.createPaint(this.leftStrokeColor, 0, Paint.Style.FILL, 0);
            this.leftBallStrokePaint.setShadowLayer((float)this.offset, (float)this.offset, (float)this.offset, Color.parseColor("#777777"));
            this.rightBallStrokePaint = this.createPaint(this.rightStrokeColor, 0, Paint.Style.FILL, 0);
            this.rightBallStrokePaint.setShadowLayer((float)this.offset, (float)this.offset, (float)this.offset, Color.parseColor("#777777"));
        } else {
            this.bitmapPaint = this.createBitmapPaint();
        }

    }

    private Paint createBitmapPaint() {
        Paint mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setFilterBitmap(true);
        mPaint.setDither(true);
        return mPaint;
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.drawWithOut(canvas);
        this.drawWithIn(canvas);
        this.drawLeftCircle(canvas);
        this.drawRightCircle(canvas);
    }

    @SuppressLint("WrongConstant")
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int mHeight;
        if (this.isBitmap) {
            this.leftBitmap = BitmapFactory.decodeResource(this.getResources(), this.leftBitmapResID);
            this.rightBitmap = BitmapFactory.decodeResource(this.getResources(), this.rightBitmapResID);
            this.leftSrcRect = new Rect(0, 0, this.leftBitmap.getWidth(), this.leftBitmap.getHeight());
            this.rightSrcRect = new Rect(0, 0, this.rightBitmap.getWidth(), this.rightBitmap.getHeight());
            this.leftDstRect = new Rect(0, 0, this.leftBitmap.getWidth(), this.leftBitmap.getHeight());
            this.rightDstRect = new Rect(this.getMeasuredWidth() - this.rightBitmap.getWidth(), 0, this.getMeasuredWidth(), this.rightBitmap.getHeight());
            this.leftBitmapL = 0;
            this.leftBitmapR = this.leftBitmap.getWidth();
            this.leftBitmapT = this.rightBitmapT = 0;
            this.leftBitmapB = this.rightBitmapB = this.leftBitmap.getHeight();
            this.rightBitmapL = this.getMeasuredWidth() - this.rightBitmap.getWidth();
            this.rightBitmapR = this.getMeasuredWidth();
            this.withOutRectF = new RectF((float)(this.leftBitmapL + this.leftBitmap.getWidth() / 2), (float)(this.leftBitmap.getHeight() / 2) - (float)this.pb_height / 2.0F, (float)(this.getMeasuredWidth() - this.rightBitmap.getWidth() / 2), (float)(this.leftBitmap.getHeight() / 2) + (float)this.pb_height / 2.0F);
            this.setWithInRectF();
            this.totalLength = this.getMeasuredWidth() - this.leftBitmap.getWidth() / 2 - this.rightBitmap.getWidth() / 2;
            if (MeasureSpec.getMode(heightMeasureSpec) == -2147483648) {
                mHeight = resolveSize(Math.max(this.leftBitmap.getHeight(), this.pb_height), heightMeasureSpec);
                this.setMeasuredDimension(this.getMeasuredWidth(), mHeight);
            }
        } else {
            this.leftBallX = this.ball_radius_size;
            this.rightBallX = this.getMeasuredWidth() - this.ball_radius_size - this.offset;
            this.ballY = this.ball_radius_size;
            this.withOutRectF = new RectF((float)this.ball_radius_size, (float)this.ball_radius_size - (float)this.pb_height / 2.0F, (float)(this.getMeasuredWidth() - this.ball_radius_size), (float)this.ball_radius_size + (float)this.pb_height / 2.0F);
            this.setWithInRectF();
            this.totalLength = this.getMeasuredWidth() - this.ball_radius_size * 2;
            if (MeasureSpec.getMode(heightMeasureSpec) == -2147483648) {
                mHeight = resolveSize(Math.max(this.ball_radius_size * 2 + this.offset, this.pb_height + this.offset), heightMeasureSpec);
                this.setMeasuredDimension(this.getMeasuredWidth(), mHeight);
            }
        }

    }

    private int px2sp(float pxValue) {
        float fontScale = this.getResources().getDisplayMetrics().scaledDensity;
        return (int)(pxValue / fontScale + 0.5F);
    }

    private int sp2px(float spValue) {
        float fontScale = this.getResources().getDisplayMetrics().scaledDensity;
        return (int)(spValue * fontScale + 0.5F);
    }

    public int dpToPx(int dp) {
        return (int)((float)dp * this.getResources().getDisplayMetrics().density);
    }

    public int pxToDp(int px) {
        return (int)((float)px / this.getResources().getDisplayMetrics().density);
    }

    private Paint createPaint(int paintColor, int textSize, Paint.Style style, int lineWidth) {
        Paint paint = new Paint();
        paint.setColor(paintColor);
        paint.setAntiAlias(true);
        paint.setStrokeWidth((float)lineWidth);
        paint.setDither(true);
        paint.setTextSize((float)textSize);
        paint.setStyle(style);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.ROUND);
        return paint;
    }

    private void drawWithOut(Canvas canvas) {
        canvas.drawRect(this.withOutRectF, this.withOutPaint);
    }

    private void drawWithIn(Canvas canvas) {
        canvas.drawRect(this.withInRectF, this.withInPaint);
    }

    @SuppressLint("WrongConstant")
    private void drawRightCircle(Canvas canvas) {
        if (this.isBitmap) {
            canvas.drawBitmap(this.rightBitmap, this.rightSrcRect, this.rightDstRect, this.bitmapPaint);
        } else {
            this.setLayerType(1, (Paint)null);
            canvas.drawCircle((float)this.rightBallX, (float)this.ballY, (float)this.ball_radius_size, this.rightBallStrokePaint);
            canvas.drawCircle((float)this.rightBallX, (float)this.ballY, (float)(this.ball_radius_size - this.ball_stroke_size), this.rightBallPaint);
        }

    }

    @SuppressLint("WrongConstant")
    private void drawLeftCircle(Canvas canvas) {
        if (this.isBitmap) {
            canvas.drawBitmap(this.leftBitmap, this.leftSrcRect, this.leftDstRect, this.bitmapPaint);
        } else {
            this.setLayerType(1, (Paint)null);
            canvas.drawCircle((float)this.leftBallX, (float)this.ballY, (float)this.ball_radius_size, this.leftBallStrokePaint);
            canvas.drawCircle((float)this.leftBallX, (float)this.ballY, (float)(this.ball_radius_size - this.ball_stroke_size), this.leftBallPaint);
        }

    }

    public boolean onTouchEvent(MotionEvent event) {
        switch(event.getAction()) {
            case 0:
                this.downX = (int)event.getX();
                this.currentMovingType = this.getMovingLeftOrRight(this.downX);
                if (16 == this.currentMovingType) {
                    if (this.isBitmap) {
                        this.leftBitmapL = this.downX < this.leftBitmap.getWidth() / 2 ? 0 : this.downX - this.leftBitmap.getWidth() / 2;
                        this.leftBitmapR = this.downX < this.leftBitmap.getWidth() / 2 ? this.leftBitmap.getWidth() / 2 : this.downX + this.leftBitmap.getWidth() / 2;
                        this.leftDstRect.set(this.leftBitmapL, this.leftBitmapT, this.leftBitmapR, this.leftBitmapB);
                    } else {
                        this.leftBallX = this.downX < this.ball_radius_size ? this.ball_radius_size : this.downX;
                    }
                } else if (17 == this.currentMovingType) {
                    if (this.isBitmap) {
                        this.rightBitmapL = this.downX > this.getMeasuredWidth() - this.rightBitmap.getWidth() / 2 ? this.getMeasuredWidth() - this.rightBitmap.getWidth() / 2 : this.downX - this.rightBitmap.getWidth() / 2;
                        this.rightBitmapR = this.downX > this.getMeasuredWidth() - this.rightBitmap.getWidth() / 2 ? this.getMeasuredWidth() : this.downX + this.rightBitmap.getWidth() / 2;
                        this.rightDstRect.set(this.rightBitmapL, this.rightBitmapT, this.rightBitmapR, this.rightBitmapB);
                    } else {
                        this.rightBallX = this.downX > this.getMeasuredWidth() - this.ball_radius_size ? this.getMeasuredWidth() - this.ball_radius_size - this.offset : this.downX;
                    }
                }
            case 1:
            default:
                break;
            case 2:
                int moveX = (int)event.getX();
                boolean flag;
                if (this.isBitmap) {
                    flag = this.leftBitmapL + this.leftBitmap.getWidth() / 2 == this.rightBitmapL + this.rightBitmap.getWidth() / 2;
                } else {
                    flag = this.leftBallX == this.rightBallX;
                }

                if (flag) {
                    if (moveX - this.lastX > 0) {
                        this.currentMovingType = 17;
                        if (this.isBitmap) {
                            this.rightBitmapL = moveX - this.rightBitmap.getWidth() / 2;
                            this.rightBitmapR = moveX + this.rightBitmap.getWidth() / 2;
                            this.rightDstRect.set(this.rightBitmapL, this.rightBitmapT, this.rightBitmapR, this.rightBitmapB);
                        } else {
                            this.rightBallX = moveX;
                        }

                        this.lastX = moveX;
                    } else {
                        this.currentMovingType = 16;
                        if (this.isBitmap) {
                            this.leftBitmapL = moveX - this.leftBitmap.getWidth() / 2;
                            this.leftBitmapR = moveX + this.leftBitmap.getWidth() / 2;
                            this.leftDstRect.set(this.leftBitmapL, this.leftBitmapT, this.leftBitmapR, this.leftBitmapB);
                        } else {
                            this.leftBallX = moveX;
                        }

                        this.lastX = moveX;
                    }
                } else {
                    this.lastX = moveX;
                    if (16 == this.currentMovingType) {
                        if (this.isBitmap) {
                            this.leftBitmapL = this.leftBitmapL - this.rightBitmapL >= 0 ? this.rightBitmapL : (moveX < this.leftBitmap.getWidth() / 2 ? 0 : moveX - this.leftBitmap.getWidth() / 2);
                            this.leftBitmapR = this.leftBitmapL - this.rightBitmapL >= 0 ? this.rightBitmapR : (moveX < this.leftBitmap.getWidth() / 2 ? this.leftBitmap.getWidth() : moveX + this.leftBitmap.getWidth() / 2);
                        } else {
                            this.leftBallX = this.leftBallX - this.rightBallX >= 0 ? this.rightBallX : (moveX < this.ball_radius_size ? this.ball_radius_size : moveX);
                        }
                    } else if (17 == this.currentMovingType) {
                        if (this.isBitmap) {
                            this.rightBitmapL = this.leftBitmapL - this.rightBitmapL >= 0 ? this.leftBitmapL : (moveX > this.getMeasuredWidth() - this.rightBitmap.getWidth() / 2 ? this.getMeasuredWidth() - this.rightBitmap.getWidth() : moveX - this.rightBitmap.getWidth() / 2);
                            this.rightBitmapR = this.leftBitmapL - this.rightBitmapL >= 0 ? this.leftBitmapR : (moveX > this.getMeasuredWidth() - this.rightBitmap.getWidth() / 2 ? this.getMeasuredWidth() : moveX + this.rightBitmap.getWidth() / 2);
                        } else {
                            this.rightBallX = this.rightBallX - this.leftBallX <= 0 ? this.leftBallX : (moveX > this.getMeasuredWidth() - this.ball_radius_size ? this.getMeasuredWidth() - this.ball_radius_size - this.offset : moveX);
                        }
                    }
                }
        }

        if (this.isBitmap) {
            if (this.leftBitmapL <= 0) {
                this.leftBitmapL = 0;
                this.leftBitmapR = this.leftBitmap.getWidth();
            }

            if (this.rightBitmapL > this.getMeasuredWidth() - this.rightBitmap.getWidth()) {
                this.rightBitmapL = this.getMeasuredWidth() - this.rightBitmap.getWidth();
                this.rightBitmapR = this.getMeasuredWidth();
            }

            if (this.leftBitmapL > this.getMeasuredWidth() - this.leftBitmap.getWidth()) {
                this.leftBitmapL = this.getMeasuredWidth() - this.leftBitmap.getWidth();
                this.leftBitmapR = this.getMeasuredWidth();
            }

            if (this.rightBitmapL <= 0) {
                this.rightBitmapL = 0;
                this.rightBitmapR = this.rightBitmap.getWidth();
            }

            this.leftDstRect.set(this.leftBitmapL, this.leftBitmapT, this.leftBitmapR, this.leftBitmapB);
            this.rightDstRect.set(this.rightBitmapL, this.rightBitmapT, this.rightBitmapR, this.rightBitmapB);
        } else {
            if (this.leftBallX < this.ball_radius_size) {
                this.leftBallX = this.ball_radius_size;
            }

            if (this.rightBallX < this.ball_radius_size) {
                this.rightBallX = this.ball_radius_size;
            }

            if (this.leftBallX > this.getMeasuredWidth() - this.ball_radius_size) {
                this.leftBallX = this.getMeasuredWidth() - this.ball_radius_size;
            }

            if (this.rightBallX > this.getMeasuredWidth() - this.ball_radius_size) {
                this.rightBallX = this.getMeasuredWidth() - this.ball_radius_size;
            }
        }

        this.setWithInRectF();
        if (this.isBitmap) {
            this.leftProgress = this.getProgressNum(this.leftBitmapL + this.leftBitmap.getWidth() / 2);
            this.rightProgress = this.getProgressNum(this.rightBitmapL + this.rightBitmap.getWidth() / 2);
        } else {
            this.leftProgress = this.getProgressNum(this.leftBallX);
            this.rightProgress = this.getProgressNum(this.rightBallX);
        }

        if (null != this.seekBarChangeListener) {
            this.seekBarChangeListener.onProgressChanged(this.leftProgress, this.rightProgress);
        }

        this.invalidate();
        return true;
    }

    private void setWithInRectF() {
        if (this.isBitmap) {
            this.withInRectF = new RectF((float)(this.leftBitmapL + this.leftBitmap.getWidth() / 2), (float)(this.leftBitmap.getHeight() / 2) - (float)this.pb_height / 2.0F, (float)(this.rightBitmapL + this.rightBitmap.getWidth() / 2), (float)(this.leftBitmap.getHeight() / 2) + (float)this.pb_height / 2.0F);
        } else {
            this.withInRectF = new RectF((float)this.leftBallX, (float)this.ball_radius_size - (float)this.pb_height / 2.0F, (float)this.rightBallX, (float)this.ball_radius_size + (float)this.pb_height / 2.0F);
        }

    }

    public void setProgress(int leftProgress,int rightProgress){
        this.leftProgress=leftProgress;
        this.rightProgress=rightProgress;
        this.invalidate();
    }

    private int getProgressNum(int ballX) {
        int d;
        if (this.isBitmap) {
            d = this.leftBitmap.getWidth() / 2;
        } else {
            d = this.ball_radius_size;
        }

        int x = ballX - d;
        float a = (float)((this.text_right_num - this.text_left_num) / this.text_min_unit);
        float b = (float)this.totalLength / a;
        int c = Math.round((float)x / b);
        return c * this.text_min_unit + this.text_left_num;
    }

    private int getMovingLeftOrRight(int actionX) {
        if (this.isBitmap) {
            return Math.abs(this.leftBitmapL - actionX) - Math.abs(this.rightBitmapR - actionX) > 0 ? 17 : 16;
        } else {
            return Math.abs(this.leftBallX - actionX) - Math.abs(this.rightBallX - actionX) > 0 ? 17 : 16;
        }
    }

    public void setOnSeekBarChangeListener(BidirectionalSeekBar.OnSeekBarChangeListener listener) {
        this.seekBarChangeListener = listener;
    }

    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        this.invalidate();
    }

    public interface OnSeekBarChangeListener {
        void onProgressChanged(int var1, int var2);
    }

    private static class MovingBall {
        private static final int LEFT = 16;
        private static final int RIGHT = 17;

        private MovingBall() {
        }
    }

    private static enum UnitsPosition {
        GONE,
        LEFT,
        RIGHT;

        private UnitsPosition() {
        }
    }

    private static enum TextPosition {
        GONE,
        BELOW,
        ABOVE;

        private TextPosition() {
        }
    }
}

